package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.FlightMgr;
import ejb.dto.FlightDTO;


@ManagedBean
@RequestScoped
public class FlightBean {
	
	public FlightBean() {
		flight = new FlightDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private FlightMgr flightMgr; 
	
	@SuppressWarnings("unused")
	private List<FlightDTO> allFlights;
	
	private int flightId;
	private FlightDTO flight;
	private String errorMessage;
	
	/**
	 * Delete Flight from database
	 * @param id
	 * @return redirect to page <code>manageFlights</code>
	 */
	public String deleteFlight(int id) {
		boolean result = flightMgr.deleteFlight(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageFlights?faces-redirect=true";
	}
	
	/**
	 * Update Flight in database
	 * @return redirect to page <code>manageFlights</code>
	 */
	public String updateFlight() {
		boolean result = flightMgr.updateFlight(flight);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageFlights?faces-redirect=true";
	}
	
	/**
	 * Insert Flight in database
	 * @return redirect to page <code>manageFlights</code>
	 */
	public String insertFlight() {
		boolean result = flightMgr.insertFlight(flight);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageFlights?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public FlightDTO getFlight() {
		return flight;
	}

	public void setFlight(FlightDTO flight) {
		this.flight = flight;
	}

	public List<FlightDTO> getAllFlights() {
		return flightMgr.getAllFlightsDTO();
	}

	public void setAllFlights(List<FlightDTO> allFlights) {
		this.allFlights = allFlights;
	}
	
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getFlightId() {
		return flightId;
	}

	public void setFlightId(int flightId) {
		this.flightId = flightId;
		flight = flightMgr.getFlightDTO(flightId);
		if(flight == null){
			SessionBean.notFound();
		}
	}	

	
}
